﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class CrossConnOperation : ConnOperation
	{
		public CrossConnOperation(UIDocument uiDoc, bool connPointSel) : base(uiDoc)
		{
			this.connPointSel = connPointSel;
		}

		public override bool SelectDucts()
		{
			if (!this.connPointSel)
			{
				bool flag = false;
				if (this.SelectDuctsMethod != null)
				{
					string errorString = "";
					IList<Duct> source = null;
					IList<XYZ> source2 = null;
					ConnectDuctStyle fittingStyle = this.fittingStyle;
					if (fittingStyle <= ConnectDuctStyle.IntubationCross)
					{
						if (fittingStyle != ConnectDuctStyle.RoundAngleCross && fittingStyle - ConnectDuctStyle.RightAngleCross > 1)
						{
							goto IL_2C9;
						}
					}
					else if (fittingStyle - ConnectDuctStyle.RoundAngleCorssRound > 1)
					{
						if (fittingStyle != ConnectDuctStyle.SlantingCrossRound)
						{
							goto IL_2C9;
						}
						flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.CrossXConn, ref source, ref source2, ref errorString);
						goto IL_2C9;
					}
					flag = this.SelectDuctsMethod.ConnDuctsSelecting(ConnDuctSelectType.CrossConn, ref source, ref source2, ref errorString);
					IL_2C9:
					if (flag)
					{
						base.SelectedDucts = source.ToList<Duct>();
						base.SelectedPoints = source2.ToList<XYZ>();
					}
					else
					{
						base.ErrorString = errorString;
					}
				}
				return flag;
			}
			ISelectionFilter selectionFilter = new DuctFilter();
			Reference reference = base.UIDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第一根风管……");
			base.SelectedDucts.Add(base.UIDoc.Document.GetElement(reference) as Duct);
			base.SelectedPoints.Add(reference.GlobalPoint);
			new HYDuctConnect();
			int ductShape = new ConnectDuctManage().GetDuctShape(base.SelectedDucts[0]);
			if (base.SelectedDucts.Count < 1)
			{
				base.ErrorString = "没有选中风管，请选择一根风管！";
				return false;
			}
			Document document = base.UIDoc.Document;
			Selection selection = base.UIDoc.Selection;
			Reference reference2 = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第二根风管，与第一根风管在同一直线上");
			base.SelectedDucts.Add(document.GetElement(reference2) as Duct);
			base.SelectedPoints.Add(reference2.GlobalPoint);
			if (this.fittingStyle != ConnectDuctStyle.SlantingCrossRound)
			{
				reference2 = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第三根风管，与第一、二根风管垂直");
				base.SelectedDucts.Add(document.GetElement(reference2) as Duct);
				base.SelectedPoints.Add(reference2.GlobalPoint);
				reference2 = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第四根风管，与第三个风管在同一直线上，与第一、二根风管垂直");
				base.SelectedDucts.Add(document.GetElement(reference2) as Duct);
				base.SelectedPoints.Add(reference2.GlobalPoint);
			}
			else
			{
				reference2 = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第三根风管，与第一根风管间夹角应在90°-180°");
				base.SelectedDucts.Add(document.GetElement(reference2) as Duct);
				base.SelectedPoints.Add(reference2.GlobalPoint);
				reference2 = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择第四根风管，与第一根风管间夹角应在90°-180°");
				base.SelectedDucts.Add(document.GetElement(reference2) as Duct);
				base.SelectedPoints.Add(reference2.GlobalPoint);
			}
			if (!base.CheckDuctShape(base.SelectedDucts))
			{
				base.ErrorString = "选择的四根风管的截面类型必须相同！";
				return false;
			}
			if ((this.fittingStyle == ConnectDuctStyle.RoundAngleCross || this.fittingStyle == ConnectDuctStyle.RightAngleCross || this.fittingStyle == ConnectDuctStyle.IntubationCross) && ductShape != 1)
			{
				base.ErrorString = "四通截面与风管截面不匹配！";
				return false;
			}
			if ((this.fittingStyle == ConnectDuctStyle.SlantingCrossRound || this.fittingStyle == ConnectDuctStyle.IntubationCrossRound || this.fittingStyle == ConnectDuctStyle.RoundAngleCorssRound) && ductShape != 0)
			{
				base.ErrorString = "四通截面与风管截面不匹配！";
				return false;
			}
			return true;
		}

		public override bool Connect()
		{
			bool result = false;
			if (base.SelectedDucts.Count < 2)
			{
				base.ErrorString = "选择的风管个数不对！";
				return result;
			}
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			if (!connectDuctManage.BeSameShapeDucts(base.SelectedDucts))
			{
				base.ErrorString = "选择的风管截面必须一致！";
				return result;
			}
			int ductShape = connectDuctManage.GetDuctShape(base.SelectedDucts[0]);
			FamilyInstance familyInstance = null;
			string empty = string.Empty;
			List<string> list = new List<string>();
			if (base.ConnForm.Flag)
			{
				FamilySymbol familySymbol = base.UIDoc.Document.GetElementById(base.SelectedDucts[0].DuctType.RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)3, 0).MEPPartId) as FamilySymbol;
				if (familySymbol == null)
				{
					base.ErrorString = "没有可以使用的族配置，请检查Revit的族配置";
					return result;
				}
				hyductConnect.NewCrossFitting(base.SelectedDucts, base.SelectedPoints, familySymbol);
				list = null;
			}
			else
			{
				if ((this.fittingStyle == ConnectDuctStyle.RoundAngleCross || this.fittingStyle == ConnectDuctStyle.RightAngleCross || this.fittingStyle == ConnectDuctStyle.IntubationCross) && ductShape != 1)
				{
					base.ErrorString = "选择的管件要与风管匹配！";
					return result;
				}
				if ((this.fittingStyle == ConnectDuctStyle.IntubationCrossRound || this.fittingStyle == ConnectDuctStyle.RoundAngleCorssRound || this.fittingStyle == ConnectDuctStyle.SlantingCrossRound) && ductShape != 0)
				{
					base.ErrorString = "选择的管件要与风管匹配！";
					return result;
				}
				list = connectDuctManage.GetFittingNames(this.fittingStyle);
				ConnectDuctStyle fittingStyle = this.fittingStyle;
				if (fittingStyle <= ConnectDuctStyle.IntubationCross)
				{
					if (fittingStyle == ConnectDuctStyle.RoundAngleCross || fittingStyle - ConnectDuctStyle.RightAngleCross <= 1)
					{
						ConnAlignment connAlignment = ConnAlignmentFactory.GetConnAlignment(this.fittingStyle, base.UIDoc.Document);
						if (connAlignment != null)
						{
							result = connAlignment.connectAlignment(base.SelectedDucts, base.SelectedPoints);
							base.ErrorString = connAlignment.ErrorString;
						}
					}
				}
				else if (fittingStyle - ConnectDuctStyle.RoundAngleCorssRound > 1)
				{
					if (fittingStyle == ConnectDuctStyle.SlantingCrossRound)
					{
						familyInstance = hyductConnect.NewCrossFittingY(base.SelectedDucts, base.SelectedPoints, list, ref empty);
						base.ErrorString = empty;
					}
				}
				else
				{
					familyInstance = hyductConnect.NewCrossFitting(base.SelectedDucts, base.SelectedPoints, list, ref empty);
					base.ErrorString = empty;
				}
			}
			if ((familyInstance != null && list != null) || list == null)
			{
				result = true;
				base.UIDoc.Document.Regenerate();
			}
			return result;
		}

		private bool connPointSel;
	}
}
